<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#modelRepair', '模具维修信息查询')"
          v-hasPermi="['device:modelRepair:export']"
        >
          导出
        </el-button>
      </el-col>

      <!-- 分页效果 -->
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      highlight-current-row
      border
      stripe
      v-loading="loading"
      id="modelRepair"
      :data="modelRepairList"
      :height="tableH"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column
        label="#"
        type="index"
        fixed="left"
        width="40"
        align="center"
      />
      <el-table-column
        label="模具编号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="xmlMjbh"
      />
      <el-table-column
        label="模具名称"
        :show-overflow-tooltip="true"
        width="250"
        align="center"
        prop="params.mjmMjmc"
      />
      <el-table-column
        label="机台"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="xmlJtbh"
      />
      <el-table-column
        label="开始时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="xmlKssj"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.xmlKssj, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="xmlJssj"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.xmlJssj, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="用时"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
      />
      <el-table-column
        label="状态"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="xmlZtbz"
      >
        <template v-slot="scope">
          <span v-if="scope.row.xmlZtbz == 1">进行中</span>
          <span v-if="scope.row.xmlZtbz != 1">已完成</span>
        </template>
      </el-table-column>
      <el-table-column
        label="下模维修"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="xmlNg"
      >
        <template v-slot="scope">
          <el-switch
            v-model="scope.row.xmlNg"
            active-value="1"
            inactive-value="0"
            disabled
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="送修信息"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="xmlSxinf"
      />
      <el-table-column
        label="维修类型"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="xmlType"
      >
        <template v-slot="scope">
          <dict-tag :options="deviceMaintainList" :value="scope.row.xmlType" />
        </template>
      </el-table-column>
      <el-table-column
        label="问题分类"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="xmlLbdm"
      >
        <template v-slot="scope">
          <dict-tag
            :options="reasonCategoryOptions"
            :value="scope.row.xmlLbdm"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="问题描述"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="xmlYyms"
      />
      <el-table-column
        label="维修人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="xmlXmry"
      />
      <el-table-column
        label="维修信息"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="xmlWxinf"
      />
      <el-table-column
        label="开单人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="xmlJlry"
      />
      <el-table-column
        label="开单日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="xmlJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.xmlJlrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结案人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="xmlJary"
      />
      <el-table-column
        label="结案日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="xmlJarq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.xmlJarq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="450px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="98px"
      >
        <el-col :span="24">
          <el-form-item label="开单日期">
            <el-date-picker
              v-model="dateRangeXmlJlrq"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item prop="xmlMjbh">
            <span slot="label">
              <el-tooltip
                content="综合查询包含模具编号，模具名称模糊查询"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              综合查询
            </span>
            <el-input
              style="width: 240px"
              v-model="queryParams.xmlMjbh"
              placeholder="请输入模具编号或模具名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="单据状态" prop="xmlZtbz">
            <el-radio-group v-model="queryParams.xmlZtbz">
              <el-radio
                v-for="dict in ZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加模具维修作业对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        ref="queryForm"
        :model="queryParamsChild"
        v-show="showSearch"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="筛选条件">
          <el-input
            v-model="queryParamsChild.mjmMjmc"
            clearable
            size="small"
            @input="clickAddButton"
          />
        </el-form-item>

        <el-table
          highlight-current-row
          border
          stripe
          v-loading="loading"
          @scrollBottom="load"
          height="400"
          :data="ModelList"
          @selection-change="handleSelectionChangeChild"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column type="selection" width="40" align="center" />
          <el-table-column
            label="模具编号"
            width="135"
            show-overflow-tooltip
            prop="mjmMjbh"
          />
          <el-table-column
            label="模具名称"
            :show-overflow-tooltip="true"
            width="150"
            prop="mjmMjmc"
          />
          <el-table-column
            label="模具规格"
            :show-overflow-tooltip="true"
            width="150"
            prop="mjmMjgg"
          />
          <el-table-column
            label="模具吨位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="mjmDw"
          />
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改模具维修作业对话框 -->
    <el-dialog
      :title="title"
      v-model="openModel"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="24">
          <el-form-item label="机台编号" prop="xmlJtbh">
            <el-input disabled v-model="form.xmlJtbh" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="模具编号" prop="xmlMjbh">
            <el-input disabled v-model="form.xmlMjbh" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具名称" prop="mjmMjmc">
            <el-input disabled v-model="form.mjmMjmc" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间" prop="xmlKssj">
            <el-input disabled v-model="form.xmlKssj" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="结束时间" prop="xmlJssj">
            <el-input disabled v-model="form.xmlJssj" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="维修类型" prop="xmlType">
            <el-select
              v-model="form.xmlType"
              style="width: 290px"
              placeholder="请选择维修类型"
            >
              <el-option
                v-for="item in deviceMaintainList"
                :key="item.dictValue"
                :label="'[' + item.dictValue + ']' + item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="问题分类" prop="xmlLbdm">
            <el-select
              v-model="form.xmlLbdm"
              style="width: 290px"
              placeholder="请选择问题分类"
              @change="getZtlYyms"
            >
              <el-option
                v-for="item in reasonCategoryOptions"
                :key="item.dictValue"
                :label="'[' + item.dictValue + ']' + item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="22">
          <el-form-item label="问题描述" prop="xmlYyms">
            <el-input v-model="form.xmlYyms" />
          </el-form-item>
        </el-col>

        <el-col :span="1" style="margin-left: 10px">
          <el-button
            class="addButton"
            @click="clickAddButton"
            type="primary"
            icon="el-icon-plus"
            circle
          />
        </el-col>
        <el-col :span="24">
          <el-form-item label="维修人员" prop="xmlXmry">
            <el-input v-model="form.xmlXmry" />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="维修内容" prop="xmlWxinf">
            <el-input
              type="textarea"
              placeholder=""
              v-model="form.xmlWxinf"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitUpdateForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改框添加问题描述 -->
    <el-dialog
      :title="title"
      v-model="openDetail"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form
        :model="queryParamsChild"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="筛选条件" prop="params.find">
            <el-input
              clearable
              style="width: 370px"
              size="small"
              @input="clickAddButton"
            />
          </el-form-item>
        </el-col>

        <el-table
          highlight-current-row
          border
          stripe
          v-loading="loading"
          height="300"
          :data="reasonDescribeOptions"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column
            label="原因代码"
            width="135"
            prop="dictValue"
            align="center"
          />
          <el-table-column
            label="原因描述"
            :show-overflow-tooltip="true"
            align="center"
            prop="dictLabel"
          />
        </el-table>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancelDetail">取消</el-button>
            <el-button type="primary" @click="openDetail = false"
              >确认</el-button
            >
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listModelRepair,
  getModelRepair,
  delModelRepair,
  addModelRepair,
  updateModelRepair,
  exportModelRepair,
  updateUnloadModelRepair,
  finishModelRepair,
} from "@/api/device/modelRepair";
import { getMjmByBhOrMc } from "@/api/project/mjmMstr";
import { getReasonDescribleByCategory } from "@/api/system/dict/data";

export default {
  name: "ModelRepairSelect",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      tableH: document.body.clientHeight - 290,
      // 选中数组
      ids: [],
      selection: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      totalAdd: 0,
      // 模具列表
      ModelList: [],
      // 状态单选框
      ZtbzOptions: [
        {
          dictLabel: "进行中",
          dictValue: "1",
        },
        {
          dictLabel: "已完成",
          dictValue: "2",
        },
        {
          dictLabel: "全部",
          dictValue: "",
        },
      ],
      // 模具维修作业表格数据
      modelRepairList: [],
      // 维修状态
      deviceMaintainList: [],
      // 原因类别
      reasonCategoryOptions: [],
      // 原因描述
      reasonDescribeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openModel: false,
      openDetail: false,
      // 开单日期时间范围
      dateRangeXmlJlrq: [
        new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 30 * 2),
        new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 30),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 17,
        xmlMjbh: null,
        xmlZtbz: "",
        xmlJlrq: null,
      },
      queryParamsChild: {
        pageNum: 1,
        pageSize: 25,
        mjmMjmc: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getDicts("reason_category").then((response) => {
      this.reasonCategoryOptions = response.data;
    });
    this.getDicts("device_maintain_style").then((response) => {
      this.deviceMaintainList = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询模具维修作业列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      this.queryParams.params["comprehensive"] = this.queryParams.xmlMjbh;
      this.queryParams.xmlMjbh = null;
      if (null != this.dateRangeXmlJlrq && "" != this.dateRangeXmlJlrq) {
        this.queryParams.params["beginXmlJlrq"] = this.moment(
          this.dateRangeXmlJlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endXmlJlrq"] = this.moment(
          this.dateRangeXmlJlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      listModelRepair(this.queryParams).then((response) => {
        this.modelRepairList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    //load
    load() {
      if (
        this.queryParamsChild.pageNum <
        Math.ceil(this.totalAdd / this.queryParamsChild.pageSize)
      ) {
        this.queryParamsChild.pageSize = 25;
        this.queryParamsChild.pageNum++;
        getMjmByBhOrMc(this.queryParamsChild).then((response) => {
          this.ModelList = this.ModelList.concat(response.rows);
          this.totalAdd = response.total;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openModel = false;
      this.openSearch = false;
      this.reset();
    },
    // 添加问题描述取消按钮
    cancelDetail() {
      this.openDetail = false;
    },
    // 表单重置
    reset() {
      this.form = {
        xmlId: null,
        xmlJtbh: null,
        xmlMjbh: null,
        xmlXmry: null,
        xmlKssj: null,
        xmlJssj: null,
        xmlYjwcsj: null,
        xmlSxinf: null,
        xmlWxinf: null,
        xmlZtbz: 0,
        xmlNg: null,
        xmlMoeid: null,
        xmlJlry: null,
        xmlJlrq: null,
        xmlSdmid: null,
        xmlQrsj: null,
        xmlType: null,
        xmlLbdm: null,
        xmlYydm: null,
        xmlYyms: null,
        xmlJary: null,
        xmlJarq: null,
        mjmMjmc: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.modelRepairList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeXmlJlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.xmlId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleSelectionChangeChild(selectionChild) {
      this.selection = selectionChild;
    },
    getMjmByBhOrMcList() {
      this.queryParamsChild.pageNum = 1;
      this.queryParamsChild.pageSize = 25;
      getMjmByBhOrMc(this.queryParamsChild).then((res) => {
        this.ModelList = res.rows;
        this.totalAdd = res.total;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getMjmByBhOrMcList();
      this.open = true;
      this.title = "添加模具维修作业";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const xmlId = row.xmlId || this.ids;
      getModelRepair(xmlId).then((response) => {
        this.form = response.data;
        if (this.form.params != null && this.form.params != "") {
          this.form.mjmMjmc = this.form.params.mjmMjmc;
        }
        this.openModel = true;
        this.title = "修改模具维修作业";
      });
    },
    /** 下模维修操作  */
    handleUnload(row) {
      this.$confirm(
        "确认要将模具 " +
          "[" +
          row.xmlMjbh +
          "]" +
          row.params.mjmMjmc +
          " 下模维修，先行换单生产吗？请及时通知注塑部门换单生产！",
        "确认操作",
        {
          confirmButtonText: "下模维修",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return updateUnloadModelRepair(row.xmlId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("成功！请及时通知注塑部门换单生产！");
        });
    },
    /** 维修完成操作 */
    handleFinish(row) {
      this.$confirm(
        "模具编号 【" +
          row.xmlMjbh +
          "】" +
          "模具名称 【" +
          row.params.mjmMjmc +
          "】" +
          "开始时间 【" +
          row.xmlKssj +
          "】" +
          "结束时间 【" +
          +"】" +
          "用时 【" +
          row.xmlKssj +
          "】小时" +
          "确认维修完成",
        "确认操作",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return finishModelRepair(row);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("维修完成！");
        });
    },
    handleSearch() {
      this.reset();
      this.title = "查询模具维修作业";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.reset();
      for (let mjmMjbh of this.selection.map((item) => item.mjmMjbh)) {
        this.form.xmlMjbh = mjmMjbh;
        addModelRepair(this.form).then(() => {
          this.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 更新界面确认按钮 */
    submitUpdateForm() {
      updateModelRepair(this.form)
        .then(() => {
          this.msgSuccess("修改成功");
          this.openModel = false;
          this.getList();
        })
        .catch(() => {
          this.msgError("修改失败");
          this.openModel = false;
          this.getList();
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const xmlIds = row.xmlId || this.ids;
      this.$confirm(
        '是否确认删除模具维修作业编号为"' + xmlIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delModelRepair(xmlIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有模具维修作业数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportModelRepair(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 输入事件 */
    clickAddButton() {
      this.getMjmByBhOrMcList();
    },
    /** 获取问题描述 */
    getZtlYyms(data) {
      if (data) {
        getReasonDescribleByCategory(data).then((response) => {
          this.reasonDescribeOptions = response.data;
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
